<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            @click.stop="handleAdd"
            v-permission="permissionList.saveOrUpdateBtn"
            >添 加</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleBatchSubmit"
            v-permission="permissionList.submitBtn"
            >提 交</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleBatchDel"
            v-permission="permissionList.batchDelBtn"
            >删 除</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleBatchApproval"
            v-permission="permissionList.approvalBtn"
            >中心站长(副)审核通过</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click.stop="handleBatchReject"
            v-permission="permissionList.rejectBtn"
            >中心站长(副)驳回</el-button
          >
          <el-button type="primary" size="small" @click.stop="handleExcelWrite"
            >导 出</el-button
          >
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-edit"
            :size="scope.size"
            :type="scope.type"
            v-if="scope.row.checkStatus == '0' || scope.row.checkStatus == '2'"
            v-permission="permissionList.saveOrUpdateBtn"
            @click.stop="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            icon="el-icon-view"
            :size="scope.size"
            :type="scope.type"
            @click.stop="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            icon="el-icon-delete"
            :size="scope.size"
            :type="scope.type"
            v-if="scope.row.checkStatus == '0'"
            v-permission="permissionList.delBtn"
            @click.stop="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </avue-crud>
      <el-dialog
        width="70%"
        :title="dialogTitle"
        v-if="dialogVisible"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="dialogClosed"
      >
        <InvoiceCatCheckAdd
          :reportData="reportData"
          :operation-type="operationType"
          @closed="dialogClosed"
        />
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import InvoiceCatCheckAdd from "./invoice-cat-check-add.vue";
import baseCrud from "@/mixins/base-crud";
import {
  getObj,
  delObj,
  batchDelete,
  submitObj,
  approvalObj,
  rejectObj,
  exportExcel
} from "@/api/subwaystm/swstinvoicecatcheck";
import { mapGetters } from "vuex";
import { deepClone, handleExcelResponse } from "@/util/util";

export default {
  name: "swstinvoicecatcheck",
  mixins: [baseCrud("subwaystm/swstinvoicecatcheck")],
  components: {
    InvoiceCatCheckAdd,
  },
  data() {
    return {
      // 弹窗标题
      dialogTitle: "",
      // 操作类型
      operationType: "",
      // 盘点表数据
      reportData: {
        statisticsDate: "", // 盘点日期
        unuseTenBalanceNumber: 0, // 10元未使用上月结存数量
        unuseTenDispatchNumber: 0, // 10未元使用配发数量
        unuseTenDeployNumber: 0, // 10元未使用调配数量
        unuseTenDeployCompleteNumber: 0, // 10元未使用调配完数量
        unuseTenNumber: 0, // 10元未使用张数
        unuseTenMoney: 0.0, // 10元未使用金额合计
        unuseTenCheckNumber: 0, // 10元未使用实点数量
        unuseTenDiffNumber: 0, // 10元未使用差异数量
        unuseTenDiffReason: '', // 10元未使用差异原因
        unuseTwentyBalanceNumber: 0, // 20元未使用上月结存数量
        unuseTwentyDispatchNumber: 0, // 20元未使用配发数量
        unuseTwentyDeployNumber: 0, // 20元未使用调配数量
        unuseTwentyDeployCompleteNumber: 0, // 20元未使用调配完数量
        unuseTwentyNumber: 0, // 20元未使用张数
        unuseTwentyMoney: 0.0, // 20元未使用金额合计
        unuseTwentyCheckNumber: 0, // 20元未使用实点数量
        unuseTwentyDiffNumber: 0, // 20元未使用差异数量
        unuseTwentyDiffReason: '', // 20元未使用差异原因
        unuseFiftiyBalanceNumber: 0, // 50元未使用上月结存数量
        unuseFiftiyDispatchNumber: 0, // 50元未使用配发数量
        unuseFiftiyDeployNumber: 0, // 50元未使用调配数量
        unuseFiftiyDeployCompleteNumber: 0, // 50元未使用调配完数量
        unuseFiftiyNumber: 0, // 50元未使用张数
        unuseFiftiyMoney: 0.0, // 50元未使用金额合计
        unuseFiftiyCheckNumber: 0, // 50元未使用实点数量
        unuseFiftiyDiffNumber: 0, // 50元未使用差异数量
        unuseFiftiyDiffReason: '', // 50元未使用差异原因
        unuseHundredBalanceNumber: 0, // 100元未使用上月结存数量
        unuseHundredDispatchNumber: 0, // 100元未使用配发数量
        unuseHundredDeployNumber: 0, // 100元未使用调配数量
        unuseHundredDeployCompleteNumber: 0, // 100元未使用调配完数量
        unuseHundredNumber: 0, // 100元未使用张数
        unuseHundredMoney: 0.0, // 100元未使用金额合计
        unuseHundredCheckNumber: 0, // 100元未使用实点数量
        unuseHundredDiffNumber: 0, // 100元未使用差异数量
        unuseHundredDiffReason: '', // 100元未使用差异原因
        useTenBalanceNumber: 0, // 10元使用上月结存数量
        useTenDispatchNumber: 0, // 10元使用配发数量
        useTenDeployNumber: 0, // 10元使用调配数量
        useTenDeployCompleteNumber: 0, // 10元使用调配完数量
        useTenNumber: 0, // 10元使用张数
        useTenMoney: 0.0, // 10元使用金额合计
        useTenCheckNumber: 0, // 10元使用实点数量
        useTenDiffNumber: 0, // 10元使用差异数量
        useTenDiffReason: '', // 10元使用差异原因
        useTwentyBalanceNumber: 0, // 20元使用上月结存数量
        useTwentyDispatchNumber: 0, // 20元使用配发数量
        useTwentyDeployNumber: 0, // 20元使用调配数量
        useTwentyDeployCompleteNumber: 0, // 20元使用调配完数量
        useTwentyNumber: 0, // 20元使用张数
        useTwentyMoney: 0.0, // 20元使用金额合计
        useTwentyCheckNumber: 0, // 20元使用实点数量
        useTwentyDiffNumber: 0, // 20元使用差异数量
        useTwentyDiffReason: '', // 20元使用差异原因
        useFiftiyBalanceNumber: 0, // 50元使用上月结存数量
        useFiftiyDispatchNumber: 0, // 50元使用配发数量
        useFiftiyDeployNumber: 0, // 50元使用调配数量
        useFiftiyDeployCompleteNumber: 0, // 50元使用调配完数量
        useFiftiyNumber: 0, // 50元使用张数
        useFiftiyMoney: 0.0, // 50元使用金额合计
        useFiftiyCheckNumber: 0, // 50元使用实点数量
        useFiftiyDiffNumber: 0, // 50元使用差异数量
        useFiftiyDiffReason: '', // 50元使用差异原因
        useHundredBalanceNumber: 0, // 100元使用上月结存数量
        useHundredDispatchNumber: 0, // 100元使用配发数量
        useHundredDeployNumber: 0, // 100元使用调配数量
        useHundredDeployCompleteNumber: 0, // 100元使用调配完数量
        useHundredNumber: 0, // 100元使用张数
        useHundredMoney: 0.0, // 100元使用金额合计
        useHundredCheckNumber: 0, // 100元使用实点数量
        useHundredDiffNumber: 0, // 100元使用差异数量
        useHundredDiffReason: '', // 100元使用差异原因
        otherRemark: "", // 差异原因
        checkPersonName: "", // 盘点人
        checkPersonJobNo: "", // 盘点人工号
        attendName: "", // 复核人
        attendJobNo: "", // 复核人工号
      },
      // 拷贝初始值
      resetReportData: {},
      // 详情弹窗
      dialogVisible: false,
      // 检查数据接收
      checkData: {}
    };
  },
  created() {
    this.resetReportData = deepClone(this.reportData);
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_add, false),
        editBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_edit, false),
        delBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_del, false),
        saveOrUpdateBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_add_or_update, false),
        submitBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_batch_submit, false),
        batchDelBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_batch_del, false),
        approvalBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_batch_approval, false),
        rejectBtn: this.vaildData(this.permissions.stm_swstinvoicecatcheck_batch_reject, false)
      };
    },
  },
  methods: {
    handleAdd() {
      this.checkData = {};
      this.dialogTitle = "添加";
      this.operationType = "add";
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.dialogTitle = "编辑";
      this.operationType = "edit";
      getObj(row.id).then((response) => {
        let res = response.data.data;
        Object.assign(this.reportData, res);
        this.dialogVisible = true;
      });
    },
    handleView(row) {
      this.dialogTitle = "查看";
      this.operationType = "view";
      getObj(row.id).then((response) => {
        let res = response.data.data;
        Object.assign(this.reportData, res);
        this.dialogVisible = true;
      });
    },
    handleDelete(row) {
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delObj(row.id).then(response => {
          this.$message.success('删除成功')
          this.getList(this.page)
        })
      })
    },
    handleBatchSubmit() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要提交的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "0" && item.checkStatus != "2") {
          this.$message.error("请选择状态为【未提交】或【站长驳回】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "1",
      };
      submitObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    handleBatchDel() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.info("请选择要删除的记录");
        return false;
      }
      let ids = [];
      for (const item of this.$refs.crud.tableSelect) {
        if (item.checkStatus != '0') {
          this.$message.error("只能删除未提交的数据！");
          return false;
        }
        ids.push(item.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        batchDelete(ids).then(response => {
          this.$message.success('删除成功')
          this.getList(this.page)
        })
      })
    },
    handleBatchApproval() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要审核的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "1") {
          this.$message.error("请选择状态为【提交】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "3",
      };
      approvalObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    handleBatchReject() {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要驳回的记录");
        return;
      }
      for (const item of tableSelect) {
        if (item.checkStatus != "1" && item.checkStatus != "4") {
          this.$message.error("请选择状态为【提交】或【车间票务专业驳回】的数据！");
          this.$refs.crud.toggleSelection();
          return false;
        }
      }
      var ids = tableSelect.map((row) => row.id);
      const params = {
        ids: ids.join(","),
        status: "2",
      };
      rejectObj(params).then((response) => {
        this.$message.success("提交成功");
        this.getList(this.page);
      });
    },
    dialogClosed() {
      Object.assign(this.reportData, this.resetReportData);
      this.dialogVisible = false;
      this.getList(this.page);
    },
    // 导出
    async handleExcelWrite() {
      let tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length != 1) {
        this.$message.info("请选择一条长安通冠名定额发票盘点数据！");
        return;
      }
      let ids = tableSelect.map((row) => row.id);
      let res = await exportExcel(ids[0]);
      handleExcelResponse(res, "长安通冠名定额发票盘点表.xlsx");
    }
  }
};
</script>

<style lang="sass" scoped>

</style>
